<!--
 * @Description: 审批要素配置
 * @Author: wangwangwang
 * @Date: 2020-07-23 11:01:33
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-03-23 11:23:48
--> 
<template>
  <section>
    <div class="card">
      <div class="card_nav">
        <div style="margin-bottom: 20px;">
          <el-button size="small" class="bottom_all" style="margin-left: 0;" @click="add()">
            <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
            添加
          </el-button>
          <el-button size="small" class="bottom_two" @click="deleteElement()">
            <i class="icon iconfont iconicon_button_delete" style="font-size: 12px;"></i>
            删除
          </el-button>
        </div>
      </div>
      <div class="card_info" :style="style">
        <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
          <el-table-column align="center" type="selection" />
          <el-table-column label="编码" prop='code' align="center" />
          <el-table-column label="名称" prop='name' align="center" />
          <el-table-column label="数据来源" align="center">
            <template slot-scope="scope">
              <span>{{dataSource[scope.row.dataSource-1]}}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog v-dialogDrag title="选择审批要素" :visible.sync="showModalVisible">
      <el-form ref="form" :inline="true" :model="form" label-width="80px">
        <el-form-item label="编码">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="bottom_all" type="primary" @click="getAllElementData">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table ref="multipleTable" :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" tooltip-effect="dark" :data="elementsData" @selection-change="handleSelectionChange2">
        <el-table-column align="center" type="selection" />
        <el-table-column property="code" label="编码"></el-table-column>
        <el-table-column property="name" label="名称"></el-table-column>
      </el-table>
      <el-pagination :page-size="10" layout="prev, pager, next" :total="total" @current-change="currentChange"></el-pagination>
      <div slot="footer" class="dialog-footer">
        <el-button class="bottom_all" @click="showModalVisible = false">取 消</el-button>
        <el-button class="bottom_all_two" @click="addElementSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { getApprovElements } from '@/api/Signed/flows'
import { queryPage } from '@/api/Signed/DieEemente'
import { mapGetters } from 'vuex';
export default {
  props: ['processManagerId'],
  data() {
    return {
      tableData: [],
      pageSize: 10,
      total: 0,
      showModalVisible: false,
      form: {
        name: ''
      },
      elementsData: [],
      selectedKeys2: [],
      dataSource: ['数据字典', 'SQL定义', '手工录入'],
      deleteKeys: [],
      deleteElements: []
    }
  },

  methods: {
    add() {
      this.showModalVisible = true;
      const self = this;
      if (this.tableData.length) {
        const newData = this.elementsData.filter(item => this.tableData.some(items => item.id === items.approvalId))
        window.setTimeout(() => {
          if (newData.length) {
            newData.forEach(item => {
              self.$refs.multipleTable.toggleRowSelection(item, true);
            });
          }
        }, 100)
      }
    },
    deleteElement() {
      const newData = this.tableData.filter(item => !this.deleteKeys.some(items => item.id === items.id))
      this.tableData = newData;
      this.deleteElements = this.deleteKeys.filter(item => item.approvalId)
    },
    handleSelectionChange(selected) {
      this.deleteKeys = selected;
    },
    handleSelectionChange2(selectedKeys) {
      this.selectedKeys2 = selectedKeys;
    },
    initData() {
      const params = {
        processManagerId: this.processManagerId,
      }
      getApprovElements(params).then(res => {
        if (res.code === 1001) {
          this.tableData = res.data;
        }
      })
    },
    currentChange (page) {
      this.page = page
      this.getAllElementData();
    },
    getAllElementData() {
      const params = {
        page: this.page,
        pageSize: this.pageSize,
        code: this.form.code
      }
      queryPage(params).then(res => {
        if (res.code === 1001) {
          const newData = res.data.data.map(item => ({ ...item, approvalId: item.id }));
          this.elementsData = newData;
          this.total = res.data.totalRows;
        }
      })
    },
    addElementSubmit() {
      this.showModalVisible = false;
      this.tableData = this.selectedKeys2;
    }
  },
  computed: {
    ...mapGetters({
      screenWidth: 'getscreenWidth',
      screenHeight: 'getscreenHeight',
      Collapse: 'getsCollapse'
    }),
    style() {
      let num = 0;
      if (this.Collapse) {
        num = this.screenWidth - 324;
      } else {
        num = this.screenWidth - 220;
      }
      return 'width:' + num + 'px;';
    }
  },
  mounted() {
    this.initData();
    this.getAllElementData();
  },
}
</script>
<style lang="scss" scoped>
.card_bottom {
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 72px;
  .card_bottom_left {
    display: flex;
    align-items: center;
    font-size: $font_size;
  }
}
</style>
